// import { message } from "@/utils/message";
//
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useSettingStoreHook } from "@/store/modules/settings";
import { ReText } from "@/components/ReText";
const pureSetting = useSettingStoreHook();
interface TableColumnsType {
  width: any;
  minWidth: any;
  label: any;
  align: any;
  type: any;
  hideCheck: any;
  reserveSelection: any;
  sortable: any;
  hide: any;
  prop: string;
  fixed: any;
  cellRenderer: ({ row }: { row: any }) => JSX.Element;
}
function getColumnsData(
  type: string,
  tableColumns: TableColumnsType[],
  headerList: any[],
  callback: (columns: TableColumnsType[]) => void
): void {
  headerList.forEach(item => {
    tableColumns.push({
      width: item.width || "180px",
      minWidth: item.minWidth || "180px",
      label: item.label,
      align: item.align || "center",
      type: item.type || "",
      hideCheck: item.hideCheck || false,
      reserveSelection: item.reserveSelection || false,
      sortable: item.sortable || false,
      prop: item.prop || "",
      fixed: item.fixed || false,
      hide: () => false,
      cellRenderer: ({ row }) => (
        <div class="">
          {item.showValue ? (
            item.showValue.map((val, idx) => (
              <div
                class="button-group"
                style={val.cellType === "button" ? "display:inline-block;" : ""}
                key={idx}
              >
                <el-button
                  class="button-item"
                  type={getButtonType(type, val, row)}
                  v-show={isShowBtn(type, val, row)}
                  size="small"
                  {...(val.icon ? { icon: useRenderIcon(val.icon) } : {})}
                  onClick={() => handleButton(val, row)}
                >
                  {getButtonText(type, val, row)}
                </el-button>
                <el-tag
                  v-show={val.cellType === "tag"}
                  class={val.class}
                  type={getTagType(type, val, row)}
                  size="small"
                >
                  {isChinese(val.key) ? val.key : getNestedValue(row, val.key)}
                </el-tag>
                <el-image
                  v-show={val.cellType === "image"}
                  class={val.class}
                  style={val.style}
                  // src={typeof row[val.key] !== "boolean" ? row[val.key] : ""}
                  src={getImageSrc(row[val.key])}
                  fit="fill"
                  onClick={() => handleImageBtn(val, row)}
                />
                <el-switch
                  v-model={row.switch}
                  v-show={val.cellType === "switch"}
                  inline-prompt
                  active-value="1"
                  inactive-value="0"
                  active-text="开启"
                  inactive-text="关闭"
                  onChange={() => handleChangeSwitch(val, row)}
                />
                <ReText
                  class={getTextClass(type, val, row)}
                  v-show={!val.cellType}
                  onClick={() => handleTextBtn(val, row)}
                >
                  {isChinese(val.key) ? val.key : getNestedValue(row, val.key)}
                </ReText>
                {/*<span*/}
                {/*  v-show={!val.cellType}*/}
                {/*  onClick={() => handleTextBtn(val, row)}*/}
                {/*>*/}
                {/*  {isChinese(val.key) ? val.key : getNestedValue(row, val.key)}*/}
                {/*</span>*/}
              </div>
            ))
          ) : (
            <ReText
              class={getTextClass(type, item, row)}
              onClick={() => handleTextBtn(item, row)}
            >
              {isChinese(item.prop) ? item.key : getNestedValue(row, item.prop)}
            </ReText>
            // <span onClick={() => handleTextBtn(item, row)}>
            //   {isChinese(item.prop) ? item.key : getNestedValue(row, item.prop)}
            // </span>
          )}
        </div>
      )
    });
  });
  tableColumns.unshift({
    type: "selection",
    hideCheck: true,
    align: "left",
    reserveSelection: true
  } as TableColumnsType);
  callback && callback(tableColumns);
}
// 获取按钮的class
function getButtonType(type, val, row) {
  switch (type) {
    case "withdrawRecordList": //模拟提现状态
      if (row.id % 2 === 0) {
        return "danger";
      } else {
        return "success";
      }
    default:
      return val.type || "";
  }
}
// 获取按钮的文字
function getButtonText(type, val, row) {
  switch (type) {
    case "withdrawRecordList": //模拟提现状态
      if (val.tableType == "withdrawRecordList") {
        if (row.id % 2 === 0) {
          return "审核失败";
        } else {
          return "审核通过";
        }
      }
      break;
    default:
      return val.key || "";
  }
}
// 获取文本的class
function getTextClass(type, val, row) {
  switch (type) {
    case "withdrawRecordList": //模拟提现状态
      if (val.prop === "reviewStatus") {
        const textClass = row.id % 2 === 0 ? "danger-color" : "success-color";
        return textClass;
      }
      break;
    default:
      return val.class || "";
  }
}
// 获取图片可用地址
function getImageSrc(src) {
  return String(src);
}
// 点击图片
function handleImageBtn(val, row) {
  pureSetting.setUrl(row[val.key]);
  console.log(val, row);
}
// 点击文字按钮
function handleTextBtn(val, row) {
  console.log(val, row);
}
// 点击右侧的按钮
function handleButton(val: any, row: any) {
  pureSetting.setButtonInfo({ val, row });
}
// 点击切换开关
function handleChangeSwitch(val, row) {
  return;
  console.log("查看是否执行了开关", val, row);
}
// 获取tag的类型
function getTagType(type, val, row) {
  switch (type) {
    case "clientList":
      if (val.key === "status_text") {
        return row.status == 1 ? "success" : "danger";
      } else if (val.key === "subordinate_number") {
        return "info";
      }
    default:
      return "info";
  }
}
// 判断是否显示按钮
function isShowBtn(type, val, row) {
  if (val.cellType === "button") {
    return true;
  } else {
    return false;
    console.log(val, row);
  }
  // switch (type) {
  //   case "clientList":
  //   case "blindBoxList":
  //     return val.cellType === "button" ? true : false;
  //   default:
  //     return false;
  //     console.log(val, row);
  // }
}
// 判断是否中文
function isChinese(str) {
  // 使用 Unicode 编码范围来判断
  return /^[\u4e00-\u9fa5]+$/.test(str);
}
// 获取对象中的下级字段值
function getNestedValue(obj: any, keyPath: any): any {
  // 如果 keyPath 不是字符串或者是空字符串，返回 undefined 或者其他默认值
  if (typeof keyPath !== "string" || keyPath === "") {
    return undefined; // 或者根据实际需求返回其他默认值
  }

  // 如果 keyPath 不包含点符号，直接返回 obj[keyPath]
  if (!keyPath.includes(".")) {
    return obj[keyPath];
  }

  // 否则，按照点符号拆分 keyPath，并使用 reduce 访问深层属性
  return keyPath.split(".").reduce((acc, key) => {
    if (acc && typeof acc === "object" && key in acc) {
      return acc[key];
    } else {
      return undefined; // 或者根据实际需求返回其他默认值
    }
  }, obj);
}
// const handleEdit = (index: number, row) => {
//   message(`您修改了第 ${index} 行，数据为：${JSON.stringify(row)}`, {
//     type: "success"
//   });
// };
//
// const handleDelete = (index: number, row) => {
//   message(`您删除了第 ${index} 行，数据为：${JSON.stringify(row)}`);
// };

export { getColumnsData };
